using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using CommonLibrary.DBUtility;
using CommonLibrary.Model;

namespace CommonLibrary.DAL
{
    public class ComConfig
    {
        public bool Exsits(int ComID)
        {
            SqlParameter[] parameters = {
                new SqlParameter("@ComID",SqlDbType.Int)
            };
            parameters[0].Value = ComID;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                SqlDataReader dr = SqlHelper.ExecuteReader(conn, CommandType.StoredProcedure, "ComConfig_Exsits", parameters);
                if (dr.Read())
                    return true;
                else
                    return false;
            }
        }
        public bool Insert(ComConfigInfo ComConfig)
        {
            SqlParameter[] parameters = {
                new SqlParameter("@ComNo",SqlDbType.Int),
                new SqlParameter("@ComProperty",SqlDbType.NVarChar,50),
                new SqlParameter("@TelNnumber",SqlDbType.NVarChar,50),
                new SqlParameter("@LimitedCount",SqlDbType.Int),
                new SqlParameter("@SendedCount",SqlDbType.Int),
                new SqlParameter("@Enabled",SqlDbType.Bit)
            };
            parameters[0].Value = ComConfig.ComNo;
            parameters[1].Value = ComConfig.ComProperty;
            parameters[2].Value = ComConfig.TelNnumber;
            parameters[3].Value = ComConfig.LimitedCount;
            parameters[4].Value = ComConfig.SendedCount;
            parameters[5].Value = ComConfig.Enabled;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                int rowAffected = SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, "ComConfig_Insert", parameters);
                return rowAffected > 0;
            }
        }
        public bool Update(ComConfigInfo ComConfig)
        {
            SqlParameter[] parameters = {
                new SqlParameter("@ComID",SqlDbType.Int),
                new SqlParameter("@ComNo",SqlDbType.Int),
                new SqlParameter("@ComProperty",SqlDbType.NVarChar,50),
                new SqlParameter("@TelNnumber",SqlDbType.NVarChar,50),
                new SqlParameter("@LimitedCount",SqlDbType.Int),
                new SqlParameter("@SendedCount",SqlDbType.Int),
                new SqlParameter("@Enabled",SqlDbType.Bit)
            };
            parameters[0].Value = ComConfig.ComID;
            parameters[1].Value = ComConfig.ComNo;
            parameters[2].Value = ComConfig.ComProperty;
            parameters[3].Value = ComConfig.TelNnumber;
            parameters[4].Value = ComConfig.LimitedCount;
            parameters[5].Value = ComConfig.SendedCount;
            parameters[6].Value = ComConfig.Enabled;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                int rowAffected = SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, "ComConfig_Update", parameters);
                return rowAffected > 0;
            }
        }
        public bool UpdateSendedCount(ComConfigInfo ComConfig)
        {
            SqlParameter[] parameters = {
                new SqlParameter("@ComNo",SqlDbType.Int),
                new SqlParameter("@SendedCount",SqlDbType.Int)
            };
            parameters[0].Value = ComConfig.ComNo;
            parameters[1].Value = ComConfig.SendedCount;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                int rowAffected = SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, "ComConfig_UpdateSendedCount", parameters);
                return rowAffected > 0;
            }
        }
        public bool UpdateNoSendedCount(ComConfigInfo ComConfig)
        {
            SqlParameter[] parameters = {
                new SqlParameter("@ComID",SqlDbType.Int),
                new SqlParameter("@ComNo",SqlDbType.Int),
                new SqlParameter("@ComProperty",SqlDbType.NVarChar,50),
                new SqlParameter("@TelNnumber",SqlDbType.NVarChar,50),
                new SqlParameter("@LimitedCount",SqlDbType.Int),
                new SqlParameter("@Enabled",SqlDbType.Bit)
            };
            parameters[0].Value = ComConfig.ComID;
            parameters[1].Value = ComConfig.ComNo;
            parameters[2].Value = ComConfig.ComProperty;
            parameters[3].Value = ComConfig.TelNnumber;
            parameters[4].Value = ComConfig.LimitedCount;
            parameters[5].Value = ComConfig.Enabled;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                int rowAffected = SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, "ComConfig_UpdateNoSendedCount", parameters);
                return rowAffected > 0;
            }
        }
        public bool Delete(int ComID)
        {
            SqlParameter[] parameters = {
                new SqlParameter("@ComID",SqlDbType.Int)
            };
            parameters[0].Value = ComID;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                int rowAffected = SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, "ComConfig_Delete", parameters);
                return rowAffected > 0;
            }
        }
        public ComConfigInfo GetComConfig(int ComID)
        {
            ComConfigInfo ComConfig = null;
            SqlParameter[] parameters = {
                new SqlParameter("@ComID",SqlDbType.Int)
            };
            parameters[0].Value = ComID;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                SqlDataReader dr = SqlHelper.ExecuteReader(conn, CommandType.StoredProcedure, "ComConfig_GetDetail", parameters);
                if (dr.Read())
                {
                    ComConfig = new ComConfigInfo();
                    ComConfig.ComID = dr.GetInt32(0);
                    ComConfig.ComNo = dr.GetInt32(1);
                    ComConfig.ComProperty = dr.GetString(2);
                    ComConfig.TelNnumber = dr.GetString(3);
                    ComConfig.LimitedCount = dr.GetInt32(4);
                    ComConfig.SendedCount = dr.GetInt32(5);
                    ComConfig.Enabled = dr.GetBoolean(6);
                }
                return ComConfig;
            }
        }
        public List<ComConfigInfo> GetList(string StrWhere)
        {
            List<ComConfigInfo> ComConfigs = new List<ComConfigInfo>();
            ComConfigInfo ComConfig = null;
            SqlParameter[] parameters = {
                new SqlParameter("@StrWhere",SqlDbType.NVarChar,4000)
            };
            parameters[0].Value = StrWhere;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                SqlDataReader dr = SqlHelper.ExecuteReader(conn, CommandType.StoredProcedure, "ComConfig_GetList", parameters);
                while (dr.Read())
                {
                    ComConfig = new ComConfigInfo();
                    ComConfig.ComID = dr.GetInt32(0);
                    ComConfig.ComNo = dr.GetInt32(1);
                    ComConfig.ComProperty = dr.GetString(2);
                    ComConfig.TelNnumber = dr.GetString(3);
                    ComConfig.LimitedCount = dr.GetInt32(4);
                    ComConfig.SendedCount = dr.GetInt32(5);
                    ComConfig.Enabled = dr.GetBoolean(6);
                    ComConfigs.Add(ComConfig);
                }
                return ComConfigs;
            }
        }

    }
}